class ChooseLevel extends GuaScene {
    constructor(game) {
        super(game)
        this.setup()
    }

    setup() {
        this.context = this.game.context
        this.currentLevel = []
        this.setupBG()
        this.setupLevel()
        this.setupInputs()

    }


    // update() {
    //     super.update();
    // }
    //
    draw() {
        super.draw()
        this.drawLevel()
        this.setupBG()
    }

// setup 系列
    setupBG() {
        let bg = PlantIcon.new(this.game, 'choose_levelbg')
        this.bg = bg
        this.addElement(bg)
    }

    setupLevel() {

        for (let i = 1; i < 6; i++) {
            for (let j = 0; j < 10; j++) {
                let y = 100 * i
                let x = 200 + 60 * j
                let level = i + '-' + j
                let l = [i, j]
                this.game.drawText(level, x, y)
                this.currentLevel.push(l)
            }
        }
        log(this.currentLevel)

    }


    setupInputs() {
        let self = this
        this.game.registerMouse(function (event, status) {
            let x = event.offsetX
            let y = event.offsetY

            if (status === 'down') {
                let r = Math.floor(y / 100) + 1
                let c = Math.floor((x - 200) / 60)
                LEVEL = []
                LEVEL.push(r)
                LEVEL.push(c)
                self.updateScene()
            }
        })

    }


// add 系列

    drawLevel() {
        this.game.drawText('选择关卡', 470, 63)
        for (const l of this.currentLevel) {
            let y = 100 * l[0]
            let x = 200 + 60 * l[1]
            let level = l[0] + '-' + l[1]
            this.game.drawText(level, x, y)
        }
        log('aaa')
    }

// update
    updateScene() {
        log('LEVEL', LEVEL)
        // if (LEVEL[0] > 1) {
        //     sleep(500).then(() => {
        //         let s = SceneEditor.new(this.game)
        //         this.game.replaceScene(s)
        //     })
        // } else {
        //     sleep(500).then(() => {
        //         let s = SceneTitle.new(this.game)
        //         this.game.replaceScene(s)
        //     })
        // }


        sleep(500).then(() => {
            let s = SceneEditor.new(this.game)
            this.game.replaceScene(s)
        })
        this.removeElement(this.bg)
        this.game.removeMouse()
    }

}

let LEVEL = [1, 1]
